<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-10-30 12:21:20
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\Traits\AttachmentTrait;
use App\Traits\MaterialTrait;
use App\Traits\SoftDeletesTrait;
use App\Traits\StorageTrait;
use App\Traits\VoucherTrait;


class PurchaseOrderDetails extends BaseModel
    {
    use SoftDeletesTrait;
    use MaterialTrait;
    use StorageTrait;
    use AttachmentTrait;
    use VoucherTrait;
    public $timestamps = true;
    public $table = "sys_purchase_order_details";
    protected $guarded = [];
    protected $casts = [
        'auxiliary_quantity' => 'decimal:2',
        'quantity'           => 'decimal:2',
        'rate'               => 'decimal:2',
        'remain_quantity'    => 'decimal:2',
        'tax_price'          => 'decimal:2',
        'price'              => 'decimal:2',
        'tax_amount'         => 'decimal:2',
        'amount'             => 'decimal:2',
        'tax'                => 'decimal:2',
    ];

    public function company()
        {
        return $this->belongsTo(Company::class);
        }

    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_with_finance');
            });
        }
    public function main()
        {
        return $this->belongsTo(PurchaseOrder::class, "main_id");
        }
    public function apply()
        {
        return $this->belongsTo(PurchaseApplyDetails::class, "purchase_apply_id");
        }
    public function department()
        {
        return $this->belongsTo(Department::class);
        }
    public function storage()
        {
        return $this->hasMany(PurchaseStorageDetails::class, "purchase_order_id")->orderBy("billdate", "desc");
        }
    }
